package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.OrgJob;
import com.ctcemti.mdm.project.sys.entity.OrgRole;
import com.ctcemti.mdm.project.sys.service.IOrgRoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@Api(value = "OrgRoleController", description = "角色控制器类")
@RequestMapping("orgrole")
public class OrgRoleController {

    @Autowired
    private IOrgRoleService orgRoleService;

    @ApiOperation(value = "分页获取角色数据list")
    @PostMapping("selectListByOrgRole")
    @ApiImplicitParam(name="entity",value = "角色实体类")
    public Result<PageInfo<OrgRole>> selectListByOrgRole(HttpServletRequest request, HttpServletResponse response , OrgRole entity){
        PageInfo<OrgRole> pageInfo = orgRoleService.selectAllOrgRoleByPage(entity);
        return new Result<PageInfo<OrgRole>>(ResultEnum.SUCCESS,pageInfo);
    }

    @ApiOperation(value = "保存角色数据")
    @PostMapping("insertOrgRole")
    @ApiImplicitParams({@ApiImplicitParam(name="entity",value = "角色数据实体类")})
    public Result<OrgRole> insertOrgRole(HttpServletRequest request, HttpServletResponse response , OrgRole entity){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        entity.setCreator(loginId);
        orgRoleService.insertSelective(entity);
        return new Result<OrgRole>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "角色挂接人员")
    @PostMapping("bindRoleAndPerson")
    @ApiImplicitParams({@ApiImplicitParam(name="roleid",value = "角色id"),@ApiImplicitParam(name="personids",value = "人员id")})
    public Result bindRoleAndPerson(HttpServletRequest request, HttpServletResponse response ,String roleid,String personids){
        orgRoleService.bindRoleAndPerson(roleid,personids);
        return new Result(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "删除角色已挂接人员")
    @PostMapping("deleteRoleAndPerson")
    @ApiImplicitParams({@ApiImplicitParam(name="roleid",value = "角色id"),@ApiImplicitParam(name="personids",value = "人员id")})
    public Result deleteRoleAndPerson(HttpServletRequest request, HttpServletResponse response ,String roleid,String personids){
        orgRoleService.unbindRoleAndPerson(roleid,personids);
        return new Result(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "角色挂接岗位")
    @PostMapping("bindRoleAndJob")
    @ApiImplicitParams({@ApiImplicitParam(name="roleid",value = "角色id"),@ApiImplicitParam(name="jobids",value = "岗位id")})
    public Result bindRoleAndJob(HttpServletRequest request, HttpServletResponse response ,String roleid,String jobids){
        orgRoleService.bindRoleAndJob(roleid,jobids);
        return new Result<OrgRole>(ResultEnum.SUCCESS,null);
    }


    @ApiOperation(value = "更新角色数据")
    @PostMapping("updateOrgRole")
    @ApiImplicitParam(name="entity",value = "角色数据实体类")
    public Result<OrgRole> updateOrgRole(HttpServletRequest request, HttpServletResponse response, OrgRole entity){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        entity.setModifiedBy(loginId);
        entity.setModifyDate(new Date());
        orgRoleService.updateByPrimaryKey(entity);
        return new Result<OrgRole>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "获取所有的角色数据")
    @PostMapping("selectAllOrgRole")
    public Result<OrgRole> selectAllOrgRole(HttpServletRequest request, HttpServletResponse response,Integer id){
        List<OrgRole> list = orgRoleService.selectAllOrgRole(null);
        return new Result<OrgRole>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据ids删除角色数据")
    @PostMapping("deleteOrgRoleByIds")
    @ApiImplicitParams({@ApiImplicitParam(name="ids",value = "主键ids串")})
    public Result<OrgRole> deleteResMethodsByIds(HttpServletRequest request, HttpServletResponse response,String ids){
        List<OrgRole> list = new ArrayList<OrgRole>();
        int flag = orgRoleService.deleteByPrimaryKeys(ids);
        return new Result<OrgRole>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("uploadFile")
    public Result<Map<String,Object>> uploadFile(HttpServletRequest request, HttpServletResponse response,Integer disciplineId) throws Exception {
        CommonsMultipartResolver commonsMultipartResolver =  new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartFile multipartFile = null;
        if(commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
            Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
            while (iterator.hasNext()){
                multipartFile= multipartHttpServletRequest.getFile(iterator.next());
            }
        }
        Map<String,Object> result = orgRoleService.uploadFile(multipartFile,response);
        return new Result<Map<String,Object>>(ResultEnum.SUCCESS,result);
    }


    @ApiOperation(value = "分页获取角色数据list")
    @PostMapping("selectJobListByCondition")
    @ApiImplicitParams({@ApiImplicitParam(name="entity",value = "岗位实体类"),@ApiImplicitParam(name="roleId",value = "角色id")})
    public Result<PageInfo<OrgJob>> selectListByOrgJob(HttpServletRequest request, HttpServletResponse response , OrgJob entity,String roleId){
        PageInfo<OrgJob> pageInfo = orgRoleService.selectAllOrgJobByPage(entity,roleId);
        return new Result<PageInfo<OrgJob>>(ResultEnum.SUCCESS,pageInfo);
    }

}
